home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Windows 95 with MFC
/
Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso
/
CODE
/
Chap06
/
DlgDemo3
/
DlgDemo3.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1996-04-05
|
8KB
|
296 lines
//***********************************************************************
//
// DlgDemo3.cpp
//
//***********************************************************************
#include <afxwin.h>
#include <stdlib.h>
#include "Resource.h"
#include "DlgDemo3.h"
CMyApp myApp;
/////////////////////////////////////////////////////////////////////////
// CMyApp member functions
BOOL CMyApp::InitInstance ()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////
// CMainWindow message map and member functions
BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_ERASEBKGND ()
ON_WM_PAINT ()
ON_COMMAND (IDM_OPTIONS_SETTINGS, OnOptionsSettings)
ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
ON_COMMAND (IDM_OPTIONS_ABOUT, OnOptionsAbout)
END_MESSAGE_MAP ()
CMainWindow::CMainWindow ()
{
m_nFillType = 1;
m_nFillColor = 2;
m_text = "Hello, MFC";
m_nHeight = 72;
m_bBold = TRUE;
m_bItalic = TRUE;
Create (NULL, "DlgDemo3", WS_OVERLAPPEDWINDOW, rectDefault, NULL,
MAKEINTRESOURCE (IDR_MAINFRAME));
}
BOOL CMainWindow::OnEraseBkgnd (CDC* pDC)
{
CRect rect;
GetClientRect (&rect);
m_nFillType == 1 ? DoGradientFill (pDC, &rect) :
DoSolidFill (pDC, &rect);
return TRUE;
}
void CMainWindow::OnPaint ()
{
CRect rect;
GetClientRect (&rect);
CPaintDC dc (this);
DoDrawText (&dc, &rect);
}
void CMainWindow::OnOptionsSettings ()
{
CSettingsDialog dlg (this);
dlg.m_nFillType = m_nFillType;
dlg.m_nFillColor = m_nFillColor;
dlg.m_text = m_text;
dlg.m_nHeight = m_nHeight;
dlg.m_bBold = m_bBold;
dlg.m_bItalic = m_bItalic;
if (dlg.DoModal () == IDOK) {
m_nFillType = dlg.m_nFillType;
m_nFillColor = dlg.m_nFillColor;
m_text = dlg.m_text;
m_nHeight = dlg.m_nHeight;
m_bBold = dlg.m_bBold;
m_bItalic = dlg.m_bItalic;
Invalidate ();
}
}
void CMainWindow::OnOptionsExit ()
{
SendMessage (WM_CLOSE, 0, 0);
}
void CMainWindow::OnOptionsAbout ()
{
CAboutDialog dlg (this);
dlg.DoModal ();
}
void CMainWindow::DoSolidFill (CDC* pDC, CRect* pRect)
{
static COLORREF crColor[5] = {
RGB (255, 0, 0),
RGB ( 0, 255, 0),
RGB ( 0, 0, 255),
RGB (255, 0, 255),
RGB ( 0, 255, 255),
};
CBrush brush (crColor[m_nFillColor]);
pDC->FillRect (pRect, &brush);
}
void CMainWindow::DoGradientFill (CDC* pDC, CRect* pRect)
{
CBrush* pBrush[64];
for (int i=0; i<64; i++) {
switch (m_nFillColor) {
case 0:
pBrush[i] = new CBrush (RGB (255 - (i * 4), 0, 0));
break;
case 1:
pBrush[i] = new CBrush (RGB (0, 255 - (i * 4), 0));
break;
case 2:
pBrush[i] = new CBrush (RGB (0, 0, 255 - (i * 4)));
break;
case 3:
pBrush[i] = new CBrush (RGB (255 - (i * 4), 0,
255 - (i * 4)));
break;
case 4:
pBrush[i] = new CBrush (RGB (0, 255 - (i * 4),
255 - (i * 4)));
break;
}
}
int nWidth = pRect->Width ();
int nHeight = pRect->Height ();
CRect rect;
for (i=0; i<nHeight; i++) {
rect.SetRect (0, i, nWidth, i + 1);
pDC->FillRect (&rect, pBrush[(i * 63) / nHeight]);
}
for (i=0; i<64; i++)
delete pBrush[i];
}
void CMainWindow::DoDrawText (CDC* pDC, CRect* pRect)
{
CFont font;
int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * m_nHeight) / 72);
font.CreateFont (nHeight, 0, 0, 0, m_bBold ? FW_BOLD : FW_NORMAL,
m_bItalic, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH |
FF_DONTCARE, "Times New Roman");
pDC->SetBkMode (TRANSPARENT);
pDC->SetTextColor (RGB (255, 255, 255));
CFont* pOldFont = pDC->SelectObject (&font);
pDC->DrawText (m_text, -1, pRect, DT_SINGLELINE | DT_CENTER |
DT_VCENTER);
pDC->SelectObject (pOldFont);
}
/////////////////////////////////////////////////////////////////////////
// CSettingsDialog message map and member functions
BEGIN_MESSAGE_MAP (CSettingsDialog, CDialog)
ON_BN_CLICKED (IDC_DEFAULTS, OnDefaults)
END_MESSAGE_MAP ()
BOOL CSettingsDialog::OnInitDialog ()
{
CDialog::OnInitDialog ();
CheckRadioButton (IDC_SOLID, IDC_GRADIENT, IDC_SOLID + m_nFillType);
CheckRadioButton (IDC_RED, IDC_CYAN, IDC_RED + m_nFillColor);
ctlText ().SetWindowText (m_text);
ctlText ().LimitText (32);
char szHeight[4];
_itoa (m_nHeight, szHeight, 10);
ctlHeight ().SetWindowText (szHeight);
ctlHeight ().LimitText (3);
ctlBold ().SetCheck (m_bBold ? BST_CHECKED : BST_UNCHECKED);
ctlItalic ().SetCheck (m_bItalic ? BST_CHECKED : BST_UNCHECKED);
return TRUE;
}
void CSettingsDialog::OnOK ()
{
m_nFillType = GetCheckedRadioButton (IDC_SOLID, IDC_GRADIENT);
m_nFillColor = GetCheckedRadioButton (IDC_RED, IDC_CYAN);
ctlText ().GetWindowText (m_text);
char szHeight[4];
ctlHeight ().GetWindowText (szHeight, 4);
m_nHeight = atoi (szHeight);
if ((m_nHeight < 8) || (m_nHeight > 144)) {
MessageBox ("Text height must be an integer from 8 through 144");
ctlHeight ().SetFocus ();
ctlHeight ().SetSel (0, -1);
return;
}
m_bBold = (ctlBold ().GetCheck () == BST_CHECKED) ? TRUE : FALSE;
m_bItalic = (ctlItalic ().GetCheck () == BST_CHECKED) ? TRUE : FALSE;
CDialog::OnOK ();
}
void CSettingsDialog::OnDefaults ()
{
CheckRadioButton (IDC_SOLID, IDC_GRADIENT, IDC_GRADIENT);
CheckRadioButton (IDC_RED, IDC_CYAN, IDC_BLUE);
ctlText ().SetWindowText ("Hello, MFC");
ctlHeight ().SetWindowText ("72");
ctlBold ().SetCheck (BST_CHECKED);
ctlItalic ().SetCheck (BST_CHECKED);
}
int CSettingsDialog::GetCheckedRadioButton (int nFirst, int nLast)
{
int nCount = nLast - nFirst + 1;
for (int i=0; i<nCount; i++)
if (IsDlgButtonChecked (nFirst + i))
return i;
return -1;
}
/////////////////////////////////////////////////////////////////////////
// CAboutDialog message map and member functions
BEGIN_MESSAGE_MAP (CAboutDialog, CDialog)
ON_WM_PAINT ()
END_MESSAGE_MAP ()
BOOL CAboutDialog::OnInitDialog ()
{
CDialog::OnInitDialog ();
CStatic* pStatic = (CStatic*) GetDlgItem (IDC_ICONRECT);
pStatic->GetWindowRect (&m_rect);
pStatic->DestroyWindow ();
ScreenToClient (&m_rect);
return TRUE;
}
void CAboutDialog::OnPaint ()
{
CPaintDC dc (this);
HICON hIcon = (HICON) ::GetClassLong (AfxGetMainWnd ()->m_hWnd,
GCL_HICON);
if (hIcon != NULL) {
CDC dcMem;
dcMem.CreateCompatibleDC (&dc);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap (&dc, 32, 32);
CBitmap* pOldBitmap = dcMem.SelectObject (&bitmap);
CBrush brush (::GetSysColor (COLOR_3DFACE));
dcMem.FillRect (CRect (0, 0, 32, 32), &brush);
dcMem.DrawIcon (0, 0, hIcon);
dc.StretchBlt (m_rect.left, m_rect.top, m_rect.Width(),
m_rect.Height (), &dcMem, 0, 0, 32, 32, SRCCOPY);
dcMem.SelectObject (pOldBitmap);
}
}